import threading
import urllib3
from bs4 import BeautifulSoup

target_url = 'https://p.weather.com.cn/zrds/index.shtml'

header = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

urls = []
threads = []
#解析网页数据
def parseData(data):
    count=0
    soup = BeautifulSoup(data,"lxml")
    images = soup.select("img")
    for img in images:
        src = img["src"]
        urls.append(src)
        #下载图片
        if src not in urls:
            count=count+1
            T = threading.Thread(target=download,args=(src,count))
            T.setDaemon(False)
            T.start()
            threads.apped(T)

def download(url,count):
    try:
        if (url[len(url)-4] =="."):
            ext = url[len(url)-4:]
        else:
            ext = ".png"
            req = urllib3.PoolManager().request('GET',url,headers=header)
            data = req.data
            img_file =open("images\\"+str(count)+ext,"wb")
            img_file.write(data)
            img_file.close()
            print("图片已下载："+str(count)+ext)
    except Ellipsis as err:
        print(err)


def requestData():
    http = urllib3.PoolManager()
    rq = http.request('GET',target_url,headers=header)
    print("获得网页返回的内容：",rq.data.decode('utf-8'))
    return rq.data.decode('utf-8')
if __name__=='__main__':
    data=requestData()